package com.yh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.entity.Pic;
import com.yh.services.PicService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class PicController {

    @Resource
    private PicService picServiceImpl;

    @RequestMapping("/toIndex")
    public ModelAndView toIndex(){
        return new ModelAndView("index");
    }


    @RequestMapping("/addImage")
    @ResponseBody
    public String addPic(Pic pic, MultipartFile file){
        int r=picServiceImpl.addPic(file,pic);
        if (r>0){
            return "true";
        }else {
            return "fail";
        }
    }

    @RequestMapping("/showImages")
    @ResponseBody
    public Page<Pic> getAllPic(Integer pageIndex,Integer pageSize,Pic pic,Integer flag){
        return picServiceImpl.getAllPic(flag,pic,pageIndex,pageSize);
    }


    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updateStatus(Pic pic){
        int r = picServiceImpl.updateStatus(pic);
        if (r>0){
            return "true";
        }else {
            return "fail";
        }
    }

    @RequestMapping("/getPic")
    public String getPicById(Integer id, Model model){
        Pic pic=picServiceImpl.getPicById(id);
        model.addAttribute("image",pic);
        return "picture-edit";
    }

    @RequestMapping("/updateImage")
    @ResponseBody
    public String updateImage(Pic pic,MultipartFile file){
        int r=picServiceImpl.updatePic(file,pic);
        if (r>0){
            return "true";
        }else {
            return "fail";
        }
    }

    @RequestMapping("/getCarousel")
    @ResponseBody
    public List<Pic> getPic(){
        return picServiceImpl.getPic();
    }

}
